package com.xuecheng.checkcode.service.impl;

import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.checkcode.model.CheckCodeParamsDto;
import com.xuecheng.checkcode.model.CheckCodeResultDto;
import com.xuecheng.checkcode.service.SendCheckCodeService;
import com.xuecheng.checkcode.util.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import java.util.concurrent.TimeUnit;

@Service
public class SendCheckCodeServiceImpl implements SendCheckCodeService {

    public final Long CODE_TTL = 120L;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 给邮箱发送验证码
     *
     * @param email
     * @param code
     */
    @Override
    public void sendEmail(String email, String code) {

        //1.给用户发送验证码
        try {
            MailUtil.sendTestMail(email,code);
        } catch (MessagingException e) {
            XueChengPlusException.cast("发送验证码失败，请稍后重试");
        }
        //2.将验证码保存到redis中，ttl设置为120s
        redisTemplate.opsForValue().set(email,code,CODE_TTL, TimeUnit.SECONDS);
    }


    /**
     * 给手机发验证码
     * @param checkCodeParamsDto
     * @return
     */
//    @Override
//    public CheckCodeResultDto sendCheckCodeByPhone(CheckCodeParamsDto checkCodeParamsDto) {
//        return null;
//    }
}
